package com.novellotus.iov.carvice.model;

import android.Manifest;
import android.widget.ImageView;

import com.jakewharton.rxbinding2.view.RxView;
import com.novellotus.iov.carvice.activity.MainActivityOld;
import com.novellotus.iov.carvice.activity.MapCarActivity;
import com.novellotus.iov.carvice.bean.RxCarListBean;
import com.novellotus.iov.carvice.bean.RxUpdate;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;
import com.tbruyelle.rxpermissions2.Permission;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * Created by Rich on 2017/9/4.
 */

public class MainModel {

    private MainActivityOld context;
    private MainModelInterface mainModelInterface;
    private final int PAGE_SIZE = 10;

    public MainModel(MainActivityOld context, MainModelInterface mainModelInterface) {
        this.context = context;
        this.mainModelInterface = mainModelInterface;
    }


    /**
     * 获取车辆列表信息
     */
    public void getList(int currentPate) {
        Observable<BaseEntity<RxCarListBean>> observable = RetrofitFactory.getInstance().followVehicleList(SharedPreferencesUtils.getKey(context)[0], currentPate, PAGE_SIZE, "");
        observable.compose(RxSchedulers.<BaseEntity<RxCarListBean>>compose())
                .subscribe(new BaseObserver<RxCarListBean>(context) {
                    @Override
                    public void onSuccess(RxCarListBean response) {
                        mainModelInterface.getCarList(response);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        mainModelInterface.getCarListFail(msg);
                    }

                    @Override
                    public void onError(String msg) {
                        mainModelInterface.getCarListFail(msg);
                    }
                });
    }


    /**
     * @param btn_map 点击地图之前先检查权限
     */
    public void setMapButton(ImageView btn_map) {
        RxView.clicks(btn_map)
                .compose(RxSchedulers.lifecycle(context)).throttleFirst(1, TimeUnit.SECONDS)
                .compose(context.rxPermissions.ensureEach(Manifest.permission.ACCESS_FINE_LOCATION))
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            context.openActivity(MapCarActivity.class);
                        } else {
                            MyUtils.showToast(context, "定位权限已经被禁止，请到设置中允许相关权限");
                        }
                    }
                });
    }

    /**
     * 获取当前未读信息数量
     */
    public void updateMessageCount() {
        Observable<BaseEntity<Integer>> observable = RetrofitFactory.getInstance().getMessageCount(SharedPreferencesUtils.getKey(context)[0]);
        observable.compose(RxSchedulers.<BaseEntity<Integer>>compose())
                .subscribe(new BaseObserver<Integer>(context) {
                    @Override
                    public void onSuccess(Integer response) {
                        mainModelInterface.getMessageCount(response);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                    }

                    @Override
                    public void onError(String msg) {

                    }

                });
    }

    /**
     * 检查是否需要更新
     */
    public void checkUpdate() {
        Observable<BaseEntity<RxUpdate>> observable = RetrofitFactory.getInstance().updateVersion(MyUtils.getVersionCode(context), SharedPreferencesUtils.getKey(context)[0]);
        observable.compose(RxSchedulers.<BaseEntity<RxUpdate>>compose())
                .subscribe(new BaseObserver<RxUpdate>(context) {
                    @Override
                    public void onSuccess(RxUpdate response) {
                        mainModelInterface.getUpdateMessage(response);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        mainModelInterface.getCarListFail(msg);
                    }

                    @Override
                    public void onError(String msg) {
                        mainModelInterface.getCarListFail(msg);
                    }

                });
    }

    public interface MainModelInterface {
        void getCarList(RxCarListBean response);

        void getCarListFail(String msg);

        void getMessageCount(int count);

        void getUpdateMessage(RxUpdate response);
    }
}
